
Imports std
Imports System.IO

Module kickbetween

    Sub Main(ByVal args() As String)

        If args.Length < 2 Then
            raiseError("argument count error, <line> input output pair0.left pair0.right ... pairN.left pairN.right. " _
                        + "if input or output is /-, use console. program will transform backslash." _
                        + "if line existing, kickbetween works in each line.", _
                        errorHandle.errorType.critical)
        Else
            Dim reader As TextReader = Nothing
            Dim writer As TextWriter = Nothing
            Dim input As String = Nothing
            Dim contant As String = Nothing
            Dim i As Int32
            Dim argstart As Int32
            Dim kickInline As Boolean = False

            If strsame(args(0), "line", False) Then
                kickInline = True
                argstart = 1
            End If
            If strsame(args(argstart), "/-", False) Then
                reader = Console.In
            Else
                reader = New StreamReader(New FileStream(args(0), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            End If
            If strsame(args(argstart + 1), "/-", False) Then
                writer = Console.Out
            Else
                writer = New StreamWriter(New FileStream(args(1), FileMode.Create, FileAccess.Write, FileShare.Read))
            End If
            assert(Not reader Is Nothing, "file no found, " + args(0))
            assert(Not writer Is Nothing, "file no found, " + args(1))

            For i = argstart + 2 To args.Length() - 1
                unescape(args(i))
            Next

            If kickInline Then
                input = reader.ReadLine
                While Not input Is Nothing
                    kickNewlineChar(input)
                    For i = argstart + 2 To args.Length() - 1 Step 2
                        If args(i).Length > 0 And args(i + 1).Length > 0 Then
                            stringUtils.kickBetween(input, args(i), args(i + 1))
                        End If
                    Next
                    kickLeftRightBlank(input)
                    writer.WriteLine(input)

                    input = reader.ReadLine
                End While
            Else
                input = reader.ReadLine
                While Not input Is Nothing
                    contant += input + newline.incode
                    input = reader.ReadLine
                End While

                For i = argstart + 2 To args.Length() - 1 Step 2
                    If Not isemptystring(args(i)) AndAlso Not isemptystring(args(i + 1)) Then
                        stringUtils.kickBetween(contant, args(i), args(i + 1))
                    End If
                Next
                writer.Write(contant)
                closestream(writer)
            End If

            closestream(reader)
            closestream(writer)
        End If
    End Sub

End Module
